package com.example.task1.ui.AllService.YouthStation;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PolicyAreaBean;
import com.example.task1.bean.StationBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class YouthStationActivity extends AppCompatActivity {

    private Banner banner;
    private RecyclerView rvList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youth_station);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("青年驿站");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN,"/prod-api/api/youth-inn/talent-policy-area/list");
        Api.send(apiRespCall, PolicyAreaBean.class, new CallBack<PolicyAreaBean>() {
            @Override
            public void success(PolicyAreaBean data) {
                List<String> collect = data.getData().stream().map(PolicyAreaBean.DataDTO::getImgUrl).collect(Collectors.toList());
                MyApplication.banner(banner, collect);
                business(data.getData());
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> Call = Api.api().get(MyApplication.TOKEN,"/prod-api/api/youth-inn/youth-inn/list");
        Api.send(Call, StationBean.class, new CallBack<StationBean>() {
            @Override
            public void success(StationBean data) {
                StationAdapter stationAdapter = new StationAdapter(YouthStationActivity.this);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(YouthStationActivity.this);
                rvList.setLayoutManager(linearLayoutManager);
                rvList.setAdapter(stationAdapter);
                stationAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void business(List<PolicyAreaBean.DataDTO> data) {
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object o, int i) {
                Intent intent = new Intent(YouthStationActivity.this,PersonActivity.class);
                PolicyAreaBean.DataDTO dataDTO = data.get(i);
                intent.putExtra("id",dataDTO.getId());
                startActivity(intent);
            }
        });
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
    }
}